package com.jingdianjichi.oss.util;


import com.jingdianjichi.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: minio文件操作工具
 * @return:
 * @Author:  sky
 * @date:  2024/11/9 16:47
 */
@Component
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    /**
     * @Description: 创建Bucket桶
     * @return: 
     * @Author:  sky
     * @date:  2024/11/9 16:49
     */
    public void createBucket(String bucket) throws Exception{
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if(!exists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }


    // 上传文件： 上传文件流、存储桶、文件名
    public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception{
        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
                .stream(inputStream, -1, 5242889L).build());
    }

    // 列出所有桶
    public List<String> getAllBuckets() throws Exception{
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    // 列出当前桶的所有文件
    public List<FileInfo> getAllFile(String bucket) throws Exception{
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
        List<FileInfo> fileInfoList = new LinkedList<>();
        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    // 下载文件：存储桶、文件名，返回下载文件流
    public InputStream downloadFile(String bucket, String objectName) throws Exception{
        return this.minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    // 删除桶
    public void deleteBucket(String bucket) throws Exception{
        this.minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    // 删除文件
    public void deleteFile(String bucket, String objectName) throws Exception{
        this.minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * 获取文件url
     */
    public String getPreviewFileUrl(String bucketName, String objectName) throws Exception{
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .method(Method.GET)
                .bucket(bucketName).object(objectName).build();
        return minioClient.getPresignedObjectUrl(args);
    }

}
